
#include "Publishers.h"

using namespace ttg;
using namespace ttg::services;

//-----------------------
//--- CommonPublisher ---
//-----------------------

CommonPublisher::CommonPublisher()
{
	this->onExitID = this->addAvatar(new PublisherInfo("ON_EXIT"));
	this->onWorldUpdateID = this->addAvatar("WORLD_UPDATE");
}

//-----------------
//--- Publisher ---
//-----------------

int Publisher::addAvatar(PublisherInfo *avatar)
{
	int res = (int)avatars.size();
	avatars.push_back(avatar);
	PublisherBase *ref;
	if ((ref = PublisherBase::getRef()) != NULL)
		ref->regPublisher(avatar);
	return res;
}

int Publisher::getAvatarID(const char *name)
{
	std::string s(name);
	for (unsigned int i = 0; i < avatars.size(); i++)
		if (s == avatars[i]->getName())
			return i;
	return -1;
}

void Publisher::sendEvent(int ID, Event *e)
{
	avatars[ID]->sendEvent(e);
}

Publisher::~Publisher()
{
	PublisherBase *ref = PublisherBase::getRef();
	for (size_t i = 0; i < avatars.size(); i++)
		if (avatars[i] != NULL)
		{
			PublisherBase::getRef()->unregPublisher(avatars[i]);
			delete avatars[i];
		}
}
